/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mobilevikings.app.debugger;

import com.mobilevikings.app.Function;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Spacer;

/**
 *
 * @author Jeroen
 */
public class DebugFunction extends Function
{

    private static Form mLogForm = new Form("DEBUGGER");
    private static DebugFunction debugFunction;
    private Command mCmdClear;

    /**
     * Gets an instance of a DebugFunction
     * @param inDisplay The Display, can be null if an instance of DebugFunction
     * is allready created once.
     * @return an instance of DebugFunction
     */
    public static DebugFunction getInstance(Display inDisplay)
    {
        synchronized (DebugFunction.class)
        {
            if (debugFunction == null)
            {
                debugFunction = new DebugFunction(inDisplay);
            }
        }
        return debugFunction;
    }
    

    private DebugFunction(Display inDisplay)
    {
        super(inDisplay);
        mCmdClear = new Command("Clear", Command.SCREEN, 1);
        mLogForm.addCommand(mCmdPrevious);
        mLogForm.addCommand(mCmdClear);
        mLogForm.setCommandListener(this);
    }

    public void show()
    {
        mDisplay.setCurrent(mLogForm);
    }

    public void commandAction(Command c, Displayable d)
    {
        if (c == mCmdPrevious)
        {
            mDisplay.setCurrent(mPreviousScreen);
        }
        else if (c == mCmdClear)
        {
            mLogForm.deleteAll();
        }
    }

    /**
     * Will add a text to the debuginformation
     * @param inText
     */
    public static void log(String inText)
    {
        mLogForm.append(inText);
        mLogForm.append(new Spacer(5, 5));
    }
}
